jQuery(document).ready(function(){
	jQuery('.inline').colorbox({inline:true, width: '1160px', height: '446px'});	
	jQuery('.inline').click(function(){
		jQuery('#ytplayer').attr('src', 'http://www.youtube.com/embed/' + jQuery(this).attr('id') + '?autoplay=0');
		var title = jQuery(this).parent().find('.tubeTitle').html();
		var description = jQuery(this).parent().find('.tubeDescription').html();
		jQuery('#addForm input[name=\'title\']').attr("value",title);
		jQuery('#addForm textarea[name=\'description\']').val(description);
		jQuery('#addForm input[name=\'id\']').attr("value",jQuery(this).attr('id'));
	});		
	jQuery('#getAllFromChannel').colorbox({inline:true, width: '800px', height: '468px'});	
	jQuery('#getAllFromChannel').click(function(){		
		var channelId = jQuery('#youtubeSearch #formKeyword').attr('value');				
		jQuery('#channelName').html(channelId);
		jQuery('#totalVideo').html(jQuery('#totalVideos').text());
	});
	jQuery('#btnSubmitChannel').click(function(){
		var subcribe = false;
		if(confirm("Do you want to subscribe from this Channel ? System will automatic get newest video from Subcribed Channel.")){
			subcribe = true;
		}		
		jQuery.colorbox.close();		
		showAjaxEffect();
		var channelId = jQuery('#youtubeSearch #formKeyword').attr('value');
		var playlistId = jQuery('#channelForm select[name=\'playlist\']').attr('value');		
		jQuery.get('/admin/tube/process', {channel: channelId, playlistId: playlistId, subcribe: subcribe, action: 'all-from-channel'}, function(data) {			
			hideAjaxEffect();	
			scrollToTop();
			if(data==''){				
				jQuery('#messageDiv').fadeIn();
				jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
				jQuery('#messageDiv > p').html('Action success ! Channel\'s videos will be gotten automatic (Maximum 49 videos per day). Please check <a href=\'/admin/mission/list\' title=\'List mission\'>list mission</a> for detail information !');					
			}else{				
				jQuery('#messageDiv').fadeIn();
				jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
				jQuery('#messageDiv > p').html(data);
				setTimeout(function() {
					jQuery('#messageDiv').fadeOut();
				}, 2000);	
			}				
		});			
		return false;
	});	
	
	jQuery('#btnSubmit').click(function(){		
		jQuery.colorbox.close();						
		jQuery('#addForm').submit();
		showAjaxEffect();		
	});	
	
	jQuery('#btnSubmitSearch').click(function(){				
		showAjaxEffect();			
	});
	
	jQuery('.paginate_button').click(function(){				
		showAjaxEffect();				
	});	
	
	
	jQuery('#addAllToList').click(function(){					
		var plId = jQuery('.playlist').val();		
		var plTitle = jQuery('.playlist option:selected').text();
		if(plId==''){
			alert('Please choose playlist to add video !');
		}else{
			var ids = '';
			var count = 0;
			jQuery('#table-result tbody tr td input[type=checkbox]').each(function (item, data) {
				if (jQuery(this).is(':checked')) {				
					ids += jQuery(this).val() + ',';
					count++;
				}
			});					
			if(count>0){				
				showAjaxEffect();				
				jQuery.get('/admin/tube/process', {ids: ids, plId: plId, action: 'add-all'}, function(data) {										
					hideAjaxEffect();
					scrollToTop();
					if(data=='true'){						
						jQuery('#table-result tbody tr td input[type=checkbox]').each(function (item, data) {
							if (jQuery(this).is(':checked')) {				
								jQuery(this).parents("tr").addClass('recent-add').removeClass('selected');
								jQuery(this).parent().removeClass('checked');
								jQuery(this).attr('checked', false);													
							}
						});
						jQuery('#messageDiv').fadeIn();
						jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
						jQuery('#messageDiv > p').html('Action success ! ' + count + ' videos have been added to playlist \"' + plTitle + '\"');
						setTimeout(function() {
							jQuery('#messageDiv').fadeOut();
						}, 2000);						
					}else{								
						jQuery('#messageDiv').fadeIn();
						jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
						jQuery('#messageDiv > p').html('Action fails ! Please try again !');
						setTimeout(function() {
							jQuery('#messageDiv').fadeOut();
						}, 2000);	
					}			
					jQuery('.checkall').parent().removeClass('checked');
					jQuery('.checkall').attr('checked', false);
				});												
			}else{
				alert('Please check some videos to do this action !');
			}				
		}
		return false;
	});			
	
	jQuery('#listCategory').change(function(){		
		jQuery('#table-result tbody tr.recent-add').removeClass('recent-add');
	});		
	jQuery('#formKeyword').bind('focusin focusout', function(e){
		var t = jQuery(this);
		if(e.type == 'focusin' && t.val() == 'Type something and hit enter...') {
			t.val('');
		} else if(e.type == 'focusout' && t.val() == '') {
			t.val('Type something and hit enter...');	
		}
	});	
});